'use strict';
// 规格参数表
module.exports = app => {
  const { STRING, BIGINT, TINYINT } = app.Sequelize;

  const MallSpecParam = app.model.define('mall_spec_param', {
    category_id: {
      type: BIGINT,
      // allowNull: false,
      comment: '类目的ID，规格组属于哪个类目下',
    },
    group_id: {
      type: BIGINT,
      allowNull: false,
      comment: '规则组Id (参数所属的规格组)',
    },
    name: {
      type: STRING,
      allowNull: false,
      comment: '参数名',
    },
    numeric: {
      type: TINYINT(1),
      allowNull: false,
      comment: '是否是数字类型参数 (true或false)',
    },
    unit: {
      type: STRING(128),
      defaultValue: '',
      comment: '数字类型参数的单位 (非数字类型可以为空)',
    },
    generic: {
      type: TINYINT(1),
      allowNull: false,
      comment: '是否是SKU通用规格 (true或false)',
    },
    searching: {
      type: TINYINT(1),
      allowNull: false,
      comment: '是否用于搜索过滤 (true或false)',
    },
    segments: {
      type: STRING(1024),
      defaultValue: '',
      comment: '区间 (数值类型参数的预设区间值，如果需要搜索，则添加分段间隔值，如CPU频率间隔：0.5-1.0)',
    },
  });

  MallSpecParam.associate = () => {
    // 存在多对一关系，所以使用belongsTo()
    MallSpecParam.associate = () => {
      app.model.MallSpecParam.belongsTo(app.model.MallCategory, { foreignKey: 'category_id' });
    };
    MallSpecParam.associate = () => {
      app.model.MallSpecParam.belongsTo(app.model.MallSpecGroup, { foreignKey: 'group_id' });
    };
  };
  return MallSpecParam;
};
